<?php

namespace App\Http\Controllers\Admin\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
use App\Services\EasySms\EasySmsService;
use App\Exceptions\ApiErrCode;
use App\Services\EasySms\AliSMSCountry;
use App\Models\Customer;
use Illuminate\Support\Facades\Cache;

class SMSController extends ApiController
{
    const LOGIN_VERIFY_CODE_CACHE_KEY_PREFIX = 'CustomerLogin:';
    const REGISTER_VERIFY_CODE_CACHE_KEY_PREFIX = 'CustomerRegister:';
    /**
     * 获取手机号码地区
     */
    public function getAreaCodes()
    {
        $areacodes=AliSMSCountry::codes();

        return $this->response(compact('areacodes'));
    }

    /**
     * 发送登录验证码
     */
    public function loginVerifyCodeSMS(Request $request)
    {
        $area_code = $request->get('area_code');
        $phone = $request->get('phone');
        // $type = (int) $request->get('type');  //0：注册验证码；其他：登录验证码
        $customer = Customer::findByAreaCodePhone($area_code, $phone);
        if(!$customer)
        {
            $code = ApiErrCode::PHONE_UNREGISTERED;
            $error = '该手机号未注册';
            return $this->response(compact('code','error'));
        }

        $verify_code = (string) \rand(100000,999999);
        $verify_key = $this->verifyCodeKey($area_code, $phone);
        $this->checkUnless(Cache::get($verify_key), trans('service.please_wait_one_min'));
        Cache::set($verify_key, $verify_code, 30);
        if (env('APP_ENV')=='production') {
            (new EasySmsService)->loginSend($phone, $verify_code, $area_code);
            return $this->response(['code' => 200]);
        } else {
            return $this->response(compact('verify_code'));
        }
    }

    private function verifyCodeKey(string $area_code, string $phone): string
    {
        return self::LOGIN_VERIFY_CODE_CACHE_KEY_PREFIX . $area_code . $phone;
    }

    /**
     * 发送注册验证码
     */
    public function registerVerifyCodeSMS(Request $request)
    {
        $area_code = $request->get('area_code');
        $phone = $request->get('phone');

        $customer = Customer::findByAreaCodePhone($area_code, $phone);
        if($customer)
        {
            $code = ApiErrCode::ADMIN_EXIST;
            $error = '该手机号已注册';
            return $this->response(compact('code','error'));
        }

        $verify_code = (string) \rand(100000,999999);
        $verify_key = $this->registerVerifyCodeKey($area_code, $phone);
        $this->checkUnless(Cache::get($verify_key), trans('service.please_wait_one_min'));
        Cache::set($verify_key, $verify_code, 30);
        if (env('APP_ENV')=='production') {
            (new EasySmsService)->registerSend($phone,$verify_code, $area_code);
            return $this->response(['code' => 200]);
        } else {
            return $this->response(compact('verify_code'));
        }
    }

    private function registerVerifyCodeKey(string $area_code, string $phone): string
    {
        return self::REGISTER_VERIFY_CODE_CACHE_KEY_PREFIX . $area_code . $phone;
    }
}
